package cn.liyiming.bbs.core.shiro;

import cn.liyiming.bbs.modular.system.model.Role;
import cn.liyiming.bbs.modular.system.model.User;
import cn.liyiming.bbs.modular.system.service.IMenuService;
import cn.liyiming.bbs.modular.system.service.IRoleService;
import cn.liyiming.bbs.modular.system.service.IUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DbRealm extends AuthorizingRealm {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户身份(用户名)
        String principal = (String)authenticationToken.getPrincipal();
        //调用Service根据用户名获取用户对象
        User user = userService.getUserByAccount(principal);

        ShiroUserFront shiroUser = new ShiroUserFront();
        shiroUser.setUser(user);
//        if (user != null) {
//            //获取对应的角色以及权限
//            Set<String> roleNames = new HashSet<>();
//            Set<String> permissionNames = new HashSet<>();
//
//            List<Role> roles = roleService.listRole(user.getId());
//
//            if(roles.size() != 0) {
//                for(int i = 0 ; i<roles.size() ; i++) {
//                    roleNames.add(roles.get(i).getName());
//                    permissionNames.addAll(menuService.getResUrlsByRoleId(roles.get(i).getId()));
//                }
////                for (Role role : roles) {
////                    System.out.println("________________"+role.getName());
////                    roleNames.add(role.getName());
////                    permissionNames.addAll(menuService.getResUrlsByRoleId(role.getId()));
////                }
//                shiroUser.setRoleNames(roleNames);
//                shiroUser.setPermissionNames(permissionNames);
//            }
//        }

        return new SimpleAuthenticationInfo(user.getAccount(), user.getPassword(), new Md5Hash(user.getSalt()), this.getClass().getName());
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        ShiroUserFront shiroUser = (ShiroUserFront)principalCollection.getPrimaryPrincipal();
        User user = userService.getUserByAccount(shiroUser.getUser().getAccount());

        shiroUser.setUser(user);
        if (user != null) {
            //获取对应的角色以及权限
            Set<String> roleNames = new HashSet<>();
            Set<String> permissionNames = new HashSet<>();

            List<Role> roles = roleService.listRole(user.getId());

            if(roles.size() != 0) {
                for(int i = 0 ; i<roles.size() ; i++) {
                    roleNames.add(roles.get(i).getName());
                    permissionNames.addAll(menuService.getResUrlsByRoleId(roles.get(i).getId()));
                }
//                for (Role role : roles) {
//                    System.out.println("________________"+role.getName());
//                    roleNames.add(role.getName());
//                    permissionNames.addAll(menuService.getResUrlsByRoleId(role.getId()));
//                }
                shiroUser.setRoleNames(roleNames);
                shiroUser.setPermissionNames(permissionNames);
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        if (shiroUser != null && shiroUser.getPermissionNames() != null && shiroUser.getPermissionNames().size() > 0) {
            info.setStringPermissions(shiroUser.getPermissionNames());
        }

        if (shiroUser != null && shiroUser.getRoleNames() != null && shiroUser.getRoleNames().size() > 0) {
            info.setRoles(shiroUser.getRoleNames());
        }

        return info;
    }
}
